

require('./common');

exports.load = function(req, res){
	
	var tableID = req.params.tableID;
	var requestID = req.params.requestID;
	var comment = req.params.comment;
	
	var strSQL = "SELECT Content, DATE_FORMAT(DateCreated,'%b %d %Y %h:%i %p')  as DateSubmitted FROM tblcomment WHERE TableID='" + tableID + "' AND RequestID = '" + requestID + "' ORDER BY ID Desc";	
	
	addComment(tableID, requestID, comment)
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		var output = '<table border=0 cellpadding=2 cellspacing=0 width="100%">';
		var users=["Stephen Cury","Harrison Barnes", "Kent Bazemore", "Andris Biedrins", "Andrew Bogut", "Festus Ezeli", "Draymond Green"];
		
		for (var index in results) {
			output += '<tr class="rightNavRow">';
			output += '<td valign="top" width="20px"><img src="/images/1.jpg" width="30px" height="40px"></td>';
			output += '<td style="border-bottom:1px Solid #d8dfea"><b>'+ users[index] + ': </b>' + results[index].Content + '<br /><br /> <font color="#777">' + results[index].DateSubmitted + '</td>';
			output += '</tr>';
		}
		output += '</table>';
		
		res.send(output)
	});
}

function addComment(tableID, requestID, comment){
		var strSQL = "INSERT INTO tblcomment (TableID, RequestID, Content, DateCreated, DateUpdated, CreatedBy, UpdatedBy) VALUES ('" + tableID + "', '" + requestID + "', '" + comment + "',Date(Now()),Date(Now()),'Stephen Curry','Stephen Curry')"
		console.log("Table ID " + tableID);
		console.log("Request ID " + requestID);
		console.log("Comment " + comment);
		
		if(tableID != "undefined" && requestID != "undefined" && comment!= "undefined"){
			conn.query(strSQL);
			console.log("done adding comment...")
		}
}
